<?php

class Sistema_ProdutoController extends App_Controller_Action
{

    private $_table     = 'PRODUTO';
    private $_pk        = 'PRO_ID';
    private $_order_col = 'PRO_ID';
    private $_order     = 'DESC';
    private $_sta_col   = 'PRO_STA';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'PRO_FILE'; 
    public  $dir = 'public/upload/produto/';
    public  $img_width  = 50;
    public  $img_height = 50;
    //public  $db = '';
     
    public function init()
    {   
        //$this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $this->obj = new Sistema_Model_DbTable_Produto();
        //if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Produto();
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('elemform','library');
        Zend_Loader::loadClass('datetools','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_produto_list.js');
        
        $where = $this->_pk . ' > 0 ';
       
        $filtroseg = $this->_getParam('filtroseg', ''); 
        $filtrocat = $this->_getParam('filtrocat', '');
        $dscfiltro = $this->_getParam('dscfiltro', '');
        
        if($filtroseg > '') $where .= ' and CTS_ID = '. $filtroseg;
        
        if($filtrocat > '') $where .= ' and CAT_ID = '. $filtrocat;
        
        if((int)$dscfiltro > 0) $where .= ' and PRO_ID = '. $dscfiltro;
        else if($dscfiltro > '') $where .= ' and (PRO_NOM like "%'. $dscfiltro.'%" or PRO_DES like "%'. $dscfiltro.'%")';
        
        $this->view->filtroseg = $filtroseg;
        $this->view->filtrocat = $filtrocat;
        $this->view->dscfiltro = $dscfiltro;
        
        //$result = $this->obj->fetchAll($this->obj->select()->where($where)->order($this->_order_col.' '.$this->_order));
        $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result,20,'filtro');
        $this->view->db = $this->db;
    }
    
    public function editAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('imagem','library');
        Zend_Loader::loadClass('elemform','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_geral.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_produto.js?12=123');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
        
        $_SESSION['dir_url'] = $_SERVER['DOCUMENT_ROOT'].$this->_urlbase.'/';
        $_SESSION['dir_uploud'] = $_SESSION['dir_url'].$this->dir;
        $_SESSION['img_width']  = $this->img_width;
        $_SESSION['img_height'] = $this->img_height;
        $_SESSION['controller'] = $this->_controller;
        
        $this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if(1){//($this->form->isValid($formData)) {
                    $id = (int) $formData[$this->_pk]; // $this->form->getValue($this->_pk);

                    $data = $formData; // $this->form->getValues();
                    
                    unset($data['files']);
                    unset($data['pro_tam']);
                    
                    $data['PRO_VAL'] = tools::money_to_float($data['PRO_VAL']);
                    $data['PRO_VALPRO'] = tools::money_to_float($data['PRO_VALPRO']);
                    $data['PRO_DATPRO'] = Datetools::dma_amd($data['PRO_DATPRO']);
                    $data['PRO_STA'] = $data['PRO_STA'];
                    $data['PRO_STAPRO'] = $data['PRO_STAPRO'];
                    $data['PRO_STAPZO'] = $data['PRO_STAPZO'];
                    
                    if($id == 0){   
                        $this->db->insert($this->_table, $data);
                        $id =  $this->db->lastInsertId();
                    }
                    else $this->db->update($this->_table, $data,$this->_pk.' = ' . $id); 
                    
                    
                    $tam = new Sistema_Model_DbTable_ProdutoHasTamanhos();
                    $tam->delete('PRO_ID = '.(int)$id);
                    foreach($formData['pro_tam'] as $ck){
                        $data_arr = array(
                            'PRO_ID' => $id,
                            'TAM_ID' => $ck,
                        );
                        $tam->insert($data_arr);
                    }

                    $this->_helper->redirector('index');
                } else {
                    $this->view->data = $formData;
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->view->data = $formData;
            }
        } else {
            $id = $this->_getParam('id', 0);
            $ins = $this->_getParam('ins', 0);
            if ($id > 0) {
                //$data = $this->obj->fetchRow($this->_pk . ' = ' . $id)->toArray();
                $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id;
                $data = $this->db->fetchRow($query);
                $data['PRO_VAL'] = tools::float_to_money($data['PRO_VAL']);
                $data['PRO_VALPRO'] = tools::float_to_money($data['PRO_VALPRO']);
                $data['PRO_DATPRO'] = Datetools::amd_dma($data['PRO_DATPRO']);
                if($ins == 1) $data['PRO_STA'] = 1;
                $this->view->data = $data;
                $_SESSION['img_key'] = $data['PRO_ID'].'_';
            }
            else{
                $data = array(
                    'PRO_NOM'   => '',
                    'PRO_STA'   => '0',
                    'CTS_ID'    => '0',
                    'CAT_ID'    => '0',
                    'MAT_ID'    => '0',
                    'FOR_ID'    => '0',
                    'GRA_ID'    => '0',
                    'GRC_ID'    => '0',
                    'GCD_ID'    => '0',
                );
                $this->db->insert($this->_table, $data);
                $id =  $this->db->lastInsertId();
                //$data = $this->db->fetchRow('SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
                //$data['PRO_STA'] = 1;
                //$this->view->data = $data;
                //$_SESSION['img_key'] = $data['PRO_ID'].'_';
                
                $urlOptions = array('controller'=>$this->_controller,'action'=>'edit','id' => $id,'ins'=>'1');
                $this->_helper->redirector->gotoRoute($urlOptions);
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {   
            //$data = array($this->_sta_col => 0);
            //$ok = (bool) $this->obj->update($data, $this->_pk . ' = ' . $id);
            //$ok = (bool) $this->obj->delete($this->_pk . ' = ' . $id);
            $ok = (bool) $this->db->delete($this->_table,$this->_pk . ' = ' . $id);
            
            if($ok){
                Zend_Loader::loadClass('imagem','library');
                imagem::delete($id,$this->dir);

                foreach (glob($this->dir.$id.'_*.*') as $file) { 
                    unlink($file);
                }

                foreach (glob($this->dir.$id.'_*_*.*') as $file) { 
                    unlink($file);
                }
            }
        }       
        //echo json_encode(array('ok' => $ok));
        $this->_helper->redirector('index');
    }
    
    public function delfotoAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $src = $this->_getParam('src', '');
        
        if($this->_urlbase > '' && $this->_urlbase != '/') $src = str_replace($this->_urlbase.'/','',$src);
        else $src = substr ($src, 1, strlen ($src));
        
        // original  w 625 h 705
        unlink(str_replace('_tb','_1z',$src));
        // home carrosel promoção // w 120 h 150 
        unlink(str_replace('_tb','_hcp',$src));
        // home lista // w 200 h 315
        unlink(str_replace('_tb','_hl',$src));
        // detalhe principal // w 325 h 405
        unlink(str_replace('_tb','_dp',$src));
        // desta lateral // w 68 h 85
        unlink(str_replace('_tb','_dl',$src));
        
        // normal
        unlink(str_replace('_tb','',$src));
        // tb
        unlink($src);
        
        echo json_encode(array('ok' => TRUE));
    }
    
    // combo box
    public function combocatAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = TRUE;
        $html = '';
        $id = $this->_getParam('id', 0);
        $seg = $this->_getParam('seg', 0);
        $cat_id = 0;
        
        if($id > 0){
            //$data = $this->obj->fetchRow($this->_pk . ' = ' . $id)->toArray();
            $data = $this->db->fetchRow('SELECT SQL_CACHE CAT_ID FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
            $cat_id = $data['CAT_ID'];
        }

        $where = ' CTS_ID = '.$seg;
        //$obj = new Sistema_Model_DbTable_Categoria();
        //$result = $obj->fetchAll($obj->select()->where($where)->order('CAT_NOM ASC'));
        $result = $this->db->fetchAll('SELECT SQL_CACHE CAT_ID,CAT_NOM FROM CATEGORIA WHERE '.$where.' ORDER BY CAT_NOM ASC');
        
        $html .= '<option value="">Nenhum</option>';
        
        foreach($result as $res){
            $selected = ($cat_id == $res['CAT_ID']) ? 'selected=""':'';
            $html .= '<option value="'.$res['CAT_ID'].'" '.$selected.'>'.$res['CAT_NOM'].'</option>';
        }
        
        echo json_encode(array('ok' => $ok,'html'=>$html));
    }
    
    public function combogradAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = TRUE;
        $html = '';
        $id = $this->_getParam('id', 0);
        $seg = $this->_getParam('seg', 0);
        $cat = $this->_getParam('cat', 0);
        $gra_id = 0;
        $count = 0;
        
        if($id > 0){
            //$data = $this->obj->fetchRow($this->_pk . ' = ' . $id)->toArray();
            $data = $this->db->fetchRow('SELECT SQL_CACHE GRA_ID FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
            $gra_id = $data['GRA_ID'];
        }
        
        $result = array();
        if($seg > 0 && $cat > 0){
            
            $list = '0';
            $where = ' CTS_ID = '.$seg.' and CAT_ID = '.$cat;
            //$grad = new Sistema_Model_DbTable_GradetamanhosHasCat();
            //$grad = $grad->fetchAll($where);
            $grad = $this->db->fetchAll('SELECT SQL_CACHE GRA_ID FROM GRADETAMANHOS_HAS_CAT WHERE '.$where);
            foreach($grad as $g){
                $list .= ','.$g['GRA_ID'];
            }
            
            //$where = ' CTS_ID = '.$seg.' and CAT_ID = '.$cat;
            $where = 'GRA_ID in('.$list.') and GRA_STA = 1';
            //$obj = new Sistema_Model_DbTable_Gradetamanhos();
            //$result = $obj->fetchAll($obj->select()->where($where)->order('GRA_NOM ASC'));
            $result = $this->db->fetchAll('SELECT SQL_CACHE GRA_ID,GRA_NOM FROM GRADETAMANHOS WHERE '.$where.' ORDER BY GRA_NOM ASC');
            
        }
        
        $html = '<option value="">Nenhum</option>';
        foreach($result as $res){
            $selected = ($gra_id == $res['GRA_ID']) ? 'selected=""':'';
            $html .= '<option value="'.$res['GRA_ID'].'" '.$selected.'>'.$res['GRA_NOM'].'</option>';
            $count = 1;
        }
        
        /*
        if($count == 0 && $cat > 0){ // pega o geral, todos
            
            $where = ' CTS_ID = '.$seg.' and CAT_ID = 0';
            $obj = new Sistema_Model_DbTable_Gradetamanhos();
            $result = $obj->fetchAll($obj->select()->where($where)->order('GRA_NOM ASC'));
            
            foreach($result as $res){
                $selected = ($gra_id == $res->GRA_ID) ? 'selected=""':'';
                $html .= '<option value="'.$res->GRA_ID.'" '.$selected.'>'.$res->GRA_NOM.'</option>';
                $count = 1;
            }
        }
        */
        
        echo json_encode(array('ok' => $ok,'html'=>$html));
        
    }
    
    public function combograd2Action() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = TRUE;
        $html = '';
        $id = $this->_getParam('id', 0);
        $seg = $this->_getParam('seg', 0);
        $cat = $this->_getParam('cat', 0);
        $gra_id = 0;
        $count = 0;
        
        if($id > 0){
            $data = $this->db->fetchRow('SELECT SQL_CACHE GRA_ID2 FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
            $gra_id = $data['GRA_ID2'];
        }
        
        $result = array();
        if($seg > 0 && $cat > 0){
            
            $list = '0';
            $where = ' CTS_ID = '.$seg.' and CAT_ID = '.$cat;
            $grad = $this->db->fetchAll('SELECT SQL_CACHE GRA_ID FROM GRADETAMANHOS_HAS_CAT WHERE '.$where);
            foreach($grad as $g){
                $list .= ','.$g['GRA_ID'];
            }
            
            $where = 'GRA_ID in('.$list.') and GRA_STA = 1';
            $result = $this->db->fetchAll('SELECT SQL_CACHE GRA_ID,GRA_NOM FROM GRADETAMANHOS WHERE '.$where.' ORDER BY GRA_NOM ASC');
            
        }
        
        $html = '<option value="">Nenhum</option>';
        foreach($result as $res){
            $selected = ($gra_id == $res['GRA_ID']) ? 'selected=""':'';
            $html .= '<option value="'.$res['GRA_ID'].'" '.$selected.'>'.$res['GRA_NOM'].'</option>';
            $count = 1;
        }
        
        echo json_encode(array('ok' => $ok,'html'=>$html));
        
    }
    
    public function combotamAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = TRUE;
        $html = '';
        $id = $this->_getParam('id', 0);
        $gra_id = $this->_getParam('grad', 0);
        $pro_tam = array();
        /*
        if($id > 0){
            $tam = New Sistema_Model_DbTable_ProdutoHasTamanhos();
            $data = $tam->fetchAll($this->_pk . ' = ' . $id)->toArray();
            foreach($data as $dat) $pro_tam[$dat['TAM_ID']] = $dat['TAM_ID']; 
        }
        
        $where = ' GRA_ID = '.$gra_id;
        $obj = new Sistema_Model_DbTable_Tamanhos();
        $result = $obj->fetchAll($obj->select()->where($where)->order(array('TAM_ORD ASC','TAM_ID ASC')));
        
        foreach($result as $res){
            $checked = (array_search($res->TAM_ID, $pro_tam) > 0) ? 'checked=""':'';
            $html .= '<input type="checkbox" '.$checked.' name="pro_tam[]" value="'.$res->TAM_ID.'"> '.$res->TAM_NOM.' - '.$res->TAM_DES.'<br>';
        }
        */
        if($gra_id > 0){
            //$gra = new Sistema_Model_DbTable_Gradetamanhos();
            //$gra = $gra->fetchRow('GRA_ID ='.$gra_id);
            $gra = $this->db->fetchRow('SELECT SQL_CACHE MDL_ID FROM GRADETAMANHOS WHERE GRA_ID = '.$gra_id);
            
            
            //$ght = new Sistema_Model_DbTable_GradetamanhosHasTamanhos();
            //$ght = $ght->fetchAll('GRA_ID ='.$gra_id);
            $ght = $this->db->fetchAll('SELECT SQL_CACHE TAM_ID FROM GRADETAMANHOS_HAS_TAMANHOS WHERE GRA_ID = '.$gra_id);
            foreach($ght as $g){
                //$tm = new Sistema_Model_DbTable_Tamanhos();
                //$tm = $tm->fetchRow('TAM_ID = '. $g['TAM_ID']);
                $tm = $this->db->fetchRow('SELECT SQL_CACHE TAM_NOM FROM TAMANHOS WHERE TAM_ID = '. $g['TAM_ID']);
                $html .= $tm['TAM_NOM'].', ';
            }

            if($html > '') $html = substr($html, 0, (strlen($html) - 2));

            if($gra['MDL_ID'] > 0){
                //$mod = new Sistema_Model_DbTable_Modelagem;
                //$mod = $mod->fetchRow('MDL_ID ='.$gra['MDL_ID']);
                $mod = $this->db->fetchRow('SELECT SQL_CACHE MDL_NOM FROM MODELAGEM WHERE MDL_ID = '.$gra['MDL_ID']);
                $html .= '<dt><label>Modelagem</label></dt><dd>'. $mod->MDL_NOM . '</dd>';
            }
        }
        echo json_encode(array('ok' => $ok,'html'=>$html));
    
    }
    
    public function combogradcorAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = TRUE;
        $html = '';
        $id = $this->_getParam('id', 0);
        $forn = $this->_getParam('forn', 0);
        $mat = $this->_getParam('mat', 0);
        $seg = $this->_getParam('seg', 0);
        $gra_id = 0;
        
        if($mat > 0){
            if($id > 0){
                //$data = $this->obj->fetchRow($this->_pk . ' = ' . $id)->toArray();
                $data = $this->db->fetchRow('SELECT SQL_CACHE GRC_ID FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
                $grc_id = $data['GRC_ID'];
            }

            //$where = ' FOR_ID = '.$forn.' and MAT_ID = '.$mat;
            $where = ' MAT_ID = '.$mat.' and CTS_ID like "%'.$seg.'%"';
            //$obj = new Sistema_Model_DbTable_Gradecor();
            //$result = $obj->fetchAll($obj->select()->where($where)->order('GRC_NOM ASC'));
            $result = $this->db->fetchAll('SELECT SQL_CACHE GRC_ID,GRC_NOM FROM GRADECOR WHERE '.$where.' ORDER BY GRC_NOM ASC');
               
            $html .= '<option value="">Nenhum</option>';
            foreach($result as $res){
                $selected = ($grc_id == $res['GRC_ID']) ? 'selected=""':'';
                $html .= '<option value="'.$res['GRC_ID'].'" '.$selected.'>'.$res['GRC_NOM'].'</option>';
            }
        }
        echo json_encode(array('ok' => $ok,'html'=>$html));
    }
    
        public function comboforAction() {
            
            $this->_helper->layout->disableLayout(); 
            $this->_helper->viewRenderer->setNoRender();  
            $ok = TRUE;
            $html = '';
            $id = $this->_getParam('id', 0);
            $seg = $this->_getParam('seg', 0);
            $cat = $this->_getParam('cat', 0);
            
            if($seg > 0 && $cat > 0){
                if($id > 0){
                    //$data = $this->obj->fetchRow($this->_pk . ' = ' . $id)->toArray();
                    $data = $this->db->fetchRow('SELECT SQL_CACHE FOR_ID FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
                    $for_id = $data['FOR_ID'];
                }

                $where = ' CTS_ID = '.$seg.' and CAT_ID = '.$cat;
                //$obj = new Sistema_Model_DbTable_FornecedorHasSetcat();
                //$result = $obj->fetchAll($obj->select()->where($where)->order('FOR_ID ASC'));
                $result = $this->db->fetchAll('SELECT SQL_CACHE FOR_ID FROM FORNECEDOR_HAS_SEGCAT WHERE '.$where.' ORDER BY FOR_ID ASC');
            
                $html .= '<option value="">Nenhum</option>';
                foreach($result as $res){
                    $selected = ($for_id == $res['FOR_ID']) ? 'selected=""':'';

                    //$fornom = new Sistema_Model_DbTable_Fornecedor();
                    //$fornom = $fornom->fetchRow('FOR_ID = '.$res['FOR_ID']);
                    $fornom = $this->db->fetchRow('SELECT SQL_CACHE FOR_NOM FROM FORNECEDOR WHERE FOR_ID = '.$res['FOR_ID']);
                    
                    $html .= '<option value="'.$res['FOR_ID'].'" '.$selected.'>'.$fornom['FOR_NOM'].'</option>';
                }
            }
            echo json_encode(array('ok' => $ok,'html'=>$html));
        }
	
	public function combomatAction() {
            $this->_helper->layout->disableLayout(); 
            $this->_helper->viewRenderer->setNoRender();  
            $ok = TRUE;
            $html = '';
            $id = $this->_getParam('id', 0);
            $for = $this->_getParam('forn', 0);
            $cat = $this->_getParam('cat', 0);
            
            if($for > 0 && $cat > 0){
            
                if($id > 0){
                    //$data = $this->obj->fetchRow($this->_pk . ' = ' . $id)->toArray();
                    $data = $this->db->fetchRow('SELECT SQL_CACHE MAT_ID FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
                    $mat_id = $data['MAT_ID'];
                }

                $where = ' FOR_ID = '.$for;
                //$obj = new Sistema_Model_DbTable_Material();
                //$result = $obj->fetchAll($obj->select()->where($where)->order('MAT_NOM ASC'));
                $result = $this->db->fetchAll('SELECT SQL_CACHE MAT_ID,MAT_NOM FROM MATERIAL WHERE '.$where.' ORDER BY MAT_NOM ASC');
            
                //$fsc = new Sistema_Model_DbTable_MaterialHasCat();

                $html .= '<option value="">Nenhum</option>';
                foreach($result as $res){
                    
                    //$r_fsc = $fsc->fetchRow('CAT_ID = '.$cat.' and MAT_ID = '.$res->MAT_ID);
                    $r_fsc = $this->db->fetchRow('SELECT SQL_CACHE MAT_ID FROM MATERIAL_HAS_CAT WHERE CAT_ID = '.$cat.' and MAT_ID = '.$res['MAT_ID']);
                    if($r_fsc['MAT_ID'] == $res['MAT_ID']) {
                        $selected = ($mat_id == $res['MAT_ID']) ? 'selected=""':'';
                        $html .= '<option value="'.$res['MAT_ID'].'" '.$selected.'>'.$res['MAT_NOM'].'</option>';
                    }
                }
            }
            echo json_encode(array('ok' => $ok,'html'=>$html));
	}
	
        public function filtrocombocatAction() {
            $this->_helper->layout->disableLayout(); 
            $this->_helper->viewRenderer->setNoRender();  
            $ok = TRUE;
            $html = '';
            $id = $this->_getParam('id', 0);

            $where = ' CTS_ID = '.$id.' and CAT_STA = 1';
            //$obj = new Sistema_Model_DbTable_Categoria();
            //$result = $obj->fetchAll($obj->select()->where($where)->order('CAT_NOM ASC'));
            $result = $this->db->fetchAll('SELECT SQL_CACHE CAT_ID,CAT_NOM FROM CATEGORIA WHERE '.$where.' ORDER BY CAT_NOM ASC');
            
            
            $html .= '<option value="">Todas Categorias</option>';

            foreach($result as $res){
                $selected = ($cat_id == $res['CAT_ID']) ? 'selected=""':'';
                $html .= '<option value="'.$res['CAT_ID'].'" '.$selected.'>'.$res['CAT_NOM'].'</option>';
            }

            echo json_encode(array('ok' => $ok,'html'=>$html));
        }
        
        public function combogradcordetAction(){
            $this->_helper->layout->disableLayout(); 
            $this->_helper->viewRenderer->setNoRender();  
            $ok = TRUE;
            $html = '';
            $id = $this->_getParam('id', 0);
            $seg = $this->_getParam('seg', 0);
            $cat = $this->_getParam('cat', 0);
            
            if($seg > 0 && $cat > 0){
            
                if($id > 0){
                    //$data = $this->obj->fetchRow($this->_pk . ' = ' . $id)->toArray();
                    $data = $this->db->fetchRow('SELECT SQL_CACHE GCD_ID FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
                    $gcd_id = $data['GCD_ID'];
                }

                $where = ' CTS_ID = '.$seg.' and CAT_ID = '.$cat;
                //$obj = new Sistema_Model_DbTable_GradecordetalheHasCat();
                //$result = $obj->fetchAll($obj->select()->where($where)->order('MAT_NOM ASC'));
                //$result = $obj->fetchAll($where);
                $result = $this->db->fetchAll('SELECT SQL_CACHE GCD_ID FROM GRADECORDETALHE_HAS_CAT WHERE '.$where);
                
                //$gcd = new Sistema_Model_DbTable_Gradecordetalhe();

                $html .= '<option value="">Nenhum</option>';
                foreach($result as $res){

                    //$r_gcd = $gcd->fetchRow('GCD_ID = '.$res->GCD_ID);
                    $r_gcd = $this->db->fetchRow('SELECT SQL_CACHE GCD_ID,GCD_NOM FROM GRADECORDETALHE WHERE GCD_ID = '.$res['GCD_ID']);
                    if($res['GCD_ID'] == $r_gcd['GCD_ID']) {
                        $selected = ($res['GCD_ID']== $gcd_id) ? 'selected=""':'';
                        $html .= '<option value="'.$res['GCD_ID'].'" '.$selected.'>'.$r_gcd['GCD_NOM'].'</option>';
                    }
                }
            }
            echo json_encode(array('ok' => $ok,'html'=>$html));
        }
        
}
